package com.xmy.cultivate.controller;

import com.aliyuncs.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.EnrollInfo;
import com.xmy.cultivate.entity.RecordCourse;
import com.xmy.cultivate.entity.SchoolNewStudent;
import com.xmy.cultivate.entity.response.EnrollRanking;
import com.xmy.cultivate.entity.response.EnrollSubjectRanking;
import com.xmy.cultivate.entity.response.EnrollTeamRanking;
import com.xmy.cultivate.entity.views.EnrollDetail;
import com.xmy.cultivate.entity.views.ResApplyGather;
import com.xmy.cultivate.excel.ExcelEnrollRanking;
import com.xmy.cultivate.service.IEnrollInfoService;
import com.xmy.cultivate.util.*;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.StringUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 招生数据 前端控制器
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
@RestController
@RequestMapping("/api/enrollInfo")
public class EnrollInfoController extends BaseController{

    @Autowired
    IEnrollInfoService iEnrollInfoService;

    /**
     * 三项
     * 排行榜
     * @param reMap
     * @return
     */
    @GetMapping("/rankingList")
    public Result rankingList(@RequestParam Map<String,String> reMap){
        String startDate = reMap.get("startDate")!=null ?reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?reMap.get("endDate"):"";
        String type = reMap.get("type")!=null ?reMap.get("type"):"";
        String schoolId = reMap.get("schoolId")!=null ?reMap.get("schoolId"):"";
        String subjectsId = reMap.get("subjectsId")!=null ?reMap.get("subjectsId"):"";

        String trackId = reMap.get("trackId");//赛道id

        List<EnrollRanking> enrollRankingList  = iEnrollInfoService.rankingList(type,startDate,endDate,schoolId,subjectsId,trackId);
        return new Result(Code.OK,new PageResult(enrollRankingList,(long)enrollRankingList.size(),1L));
    }

    /**
     * 排行榜
     * @param reMap
     * @return
     */
    @GetMapping("/rankingListExport")
    public void rankingListExport(@RequestParam Map<String,String> reMap) throws IOException {
        String startDate = reMap.get("startDate")!=null ?reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?reMap.get("endDate"):"";
        String type = reMap.get("type")!=null ?reMap.get("type"):"1";
        String schoolId = reMap.get("schoolId")!=null ?reMap.get("schoolId"):"";
        String subjectsId = reMap.get("subjectsId")!=null ?reMap.get("subjectsId"):"";
        //排名类型，1，2，3；
        String sortType = reMap.get("sortType")!=null ?reMap.get("sortType"):"2";
        String trackId = reMap.get("trackId");//赛道id

        List<EnrollRanking> enrollRankingList  = iEnrollInfoService.rankingList(type,startDate,endDate,schoolId,subjectsId,trackId);
        List<EnrollRanking> enrollRankings = iEnrollInfoService.regetRanking(enrollRankingList ,Integer.valueOf(sortType));
        String fileName = null;
        if(Integer.parseInt(sortType) == 1){
            fileName = "各校区总数排名";
        }else if(Integer.parseInt(sortType) == 2){
            fileName = "各校区退费排名";
        }else if (Integer.parseInt(sortType) ==3){
            fileName = "各校区今日新增排名";
        }
        ExcelUtil.export(fileName, enrollRankings, ExcelEnrollRanking.class, response);
//        Workbook wb = new XSSFWorkbook();
//        Sheet sheet = wb.createSheet();
//        String [] titles = "排名,老师姓名,总数,退费,今日新增".split(",");
//
//        //处理标题
//        Row row = sheet.createRow(0);
//        int titleIndex=0;
//        for (String title : titles) {
//            org.apache.poi.ss.usermodel.Cell cell = row.createCell(titleIndex++);
//            cell.setCellValue(title);
//        }
//
//        Cell cell= null;
//        int rowIndex = 1;
//        for (EnrollRanking enrollRanking:enrollRankings){
//            row =sheet.createRow(rowIndex++);
//            cell =row.createCell(0);
//            cell.setCellValue(enrollRanking.getRanking());
//            cell =row.createCell(1);
//            cell.setCellValue(enrollRanking.getTeacherName());
//            cell =row.createCell(2);
//            cell.setCellValue(enrollRanking.getAllCount().toString());
//            cell =row.createCell(3);
//            cell.setCellValue(enrollRanking.getReturnCount().toString());
//            cell =row.createCell(4);
//            cell.setCellValue(enrollRanking.getDateNewly().toString());
//        }
//        //3.完成下载
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        wb.write(os);
//        new DownloadUtils().download(os,response,"排名汇总.xlsx");
//        return new Result(Code.OK,new PageResult(enrollRankings,(long)enrollRankings.size(),1L));
    }

    @GetMapping("/incomeRankingList")
    public Result incomeRankingList(@RequestParam Map<String,String> reMap){
        String startDate = reMap.get("startDate")!=null ?reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?reMap.get("endDate"):"";
        String type = reMap.get("type")!=null ?reMap.get("type"):"";
        String schoolId = reMap.get("schoolId")!=null ?reMap.get("schoolId"):"";
        String subjectsId = reMap.get("subjectsId")!=null ?reMap.get("subjectsId"):"";

        List<EnrollRanking> enrollRankingList  = iEnrollInfoService.incomeRankingList(type,startDate,endDate,schoolId,subjectsId);
        return new Result(Code.OK,new PageResult(enrollRankingList,(long)enrollRankingList.size(),1L));

    }


    /**
     * 科目排行榜
     * @param reMap
     * @return
     */
    @GetMapping("/subjectRankingList")
    public Result subjectRankingList(@RequestParam Map reMap){

        String startDate = reMap.get("startDate")!=null ?(String) reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?(String) reMap.get("endDate"):"";
        String schoolId = reMap.get("schoolId")!=null ?(String) reMap.get("schoolId"):"";

        List<EnrollSubjectRanking> enrollSubjectRankingList = iEnrollInfoService.subjectRankingList(startDate,endDate,schoolId);

        return new Result(Code.OK,new PageResult(enrollSubjectRankingList,(long)enrollSubjectRankingList.size(),1L));
    }

    /**
     * 战队排行
     * @param reMap
     * @return
     */
    @GetMapping("/teamRankingList")
    public Result teamRankingList(@RequestParam Map reMap){
        String startDate = reMap.get("startDate")!=null ?(String) reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?(String) reMap.get("endDate"):"";
        String schoolId = reMap.get("schoolId")!=null ?(String) reMap.get("schoolId"):"";

        List<EnrollTeamRanking> enrollTeamRankingList = iEnrollInfoService.teamRankingList(startDate,endDate,schoolId);

        return new Result(Code.OK,new PageResult(enrollTeamRankingList,(long)enrollTeamRankingList.size(),1L));
    }

    /**
     * 战队排行详情
     * @param reMap
     * @return
     */
    @GetMapping("/teamRankingDetail")
    public Result teamRankingDetail(@RequestParam Map reMap){
        String startDate = reMap.get("startDate")!=null ?(String) reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?(String) reMap.get("endDate"):"";
        String schoolId = reMap.get("schoolId")!=null ?(String) reMap.get("schoolId"):"";
        String teamId = reMap.get("teamId")!=null ?(String) reMap.get("teamId"):"";


        List<EnrollTeamRanking> enrollTeamRankingList = iEnrollInfoService.teamRankingDetail(teamId,startDate,endDate,schoolId);

        return new Result(Code.OK,new PageResult(enrollTeamRankingList,(long)enrollTeamRankingList.size(),1L));
    }

    /**
     * 个人数据
     * @param reMap
     * @return
     */
    @GetMapping("/personData")
    public Result personData(@RequestParam Map<String,String> reMap){
        String startDate = reMap.get("startDate")!=null ?(String) reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?(String) reMap.get("endDate"):"";

        EnrollTeamRanking enrollTeamRanking = iEnrollInfoService.personData(this.adminModel.getId(),startDate,endDate);
        return new Result(Code.OK,enrollTeamRanking,"");
    }

    @GetMapping("/schoolNewData")
    public Result schoolNewData(@RequestParam Map<String,String> reMap){
        String startDate = reMap.get("startDate")!=null ?(String) reMap.get("startDate"):"";
        String endDate = reMap.get("endDate")!=null ?(String) reMap.get("endDate"):"";
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate),"handle_date",startDate,endDate);
        queryWrapper.groupBy("od.school_id");
        queryWrapper.isNotNull("ei.order_id");
        queryWrapper.ne("status",2);
        List<SchoolNewStudent> schoolNewStudents = iEnrollInfoService.schoolNewData(queryWrapper);
        return new Result(Code.OK,schoolNewStudents,"早安晚安");

    }


}
